using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Skywave.ComponentModel
{
    public class ClipBoardWorker
    {
        public enum ClipboardDataTypes
        {
            MicrosoftExcel = 0
        }
        public ClipBoardWorker()
        {
            mDT = new DataTable();
            mDV = mDT.DefaultView;
        }

        private DataTable mDT;
        public DataTable DataTable
        {
            get
            {
                return mDT;
            }
            set
            {
                mDT = value;
                if (mDT != null)
                    mDV = mDT.DefaultView;
            }
        }

        private DataView mDV;
        public DataView Data
        {
            get
            {
                return mDV;
            }
            set
            {
                mDV = value;
                if (mDV != null)
                    mDT = mDV.Table;
            }
        }

        public void CopyFromClipboard(ClipboardDataTypes type)
        {
            switch (type)
            {
                case ClipboardDataTypes.MicrosoftExcel:
                    string txtCB = (string)(Clipboard.GetDataObject().GetData(typeof(String)));
                    int colCount = 0;
                    string xS1, xS2;
                    string tmpCB;
                    int i, j, curCol;
                    DataRow newRow;

                    tmpCB = txtCB;
                    while (tmpCB.Length != 0)
                    {
                        i = tmpCB.IndexOf("\t");
                        j = tmpCB.IndexOf("\n");
                        if (i < 0 || i >= j) i = tmpCB.IndexOf("\r");
                        if (i > -1 && i < j) { colCount++; tmpCB = tmpCB.Remove(0, i + 1); }
                        else break;
                    }
                    mDT = new DataTable();
                    for (i = 1; i <= colCount; i++) mDT.Columns.Add("Col" + i);
                    tmpCB = txtCB;
                    while (tmpCB.Length > 0)
                    {
                        newRow = mDT.NewRow();
                        xS1 = ""; xS2 = "";
                        curCol = 1;
                        do
                        {
                            xS1 = tmpCB.Substring(0, 1);
                            tmpCB = tmpCB.Remove(0, 1);
                            if ((xS1 == "\n") || (tmpCB.Length == 0))
                            {
                                if (xS2 != "\r" && xS2 != "")
                                    newRow["Col" + curCol] = xS2;
                                for (i = 1; i <= colCount - curCol; i++) newRow["Col" + (i + curCol)] = "";
                                xS1 = ""; xS2 = "";
                                if (tmpCB.Length != 0 && tmpCB.StartsWith("\n")) tmpCB = tmpCB.Remove(0, 1);
                                break;
                            }
                            if (xS1 == "\t" || xS1 == "\r")
                            {
                                if (mDT.Columns.Contains("Col" + curCol.ToString()))
                                {
                                    newRow["Col" + curCol] = xS2;
                                    curCol += 1;
                                }
                                xS1 = ""; xS2 = "";
                            }
                            else
                            {
                                xS2 += xS1;
                            }
                        } while (true);
                        mDT.Rows.Add(newRow);
                    }
                    break;
            }
        }

        public void CopyToClipboard(ClipboardDataTypes type)
        {
            switch (type)
            {
                case ClipboardDataTypes.MicrosoftExcel:
                    {
                        //DataRow xRow;
                        StringBuilder txtCB = new StringBuilder();
                        int i;
                        if (mDT == null) return;
                        foreach (DataRowView rowV1 in mDV)
                        {
                            for (i = 1; i < mDT.Columns.Count; i++)
                            {
                                if (!Convert.IsDBNull(rowV1[i - 1])) txtCB.Append((string)(rowV1[i - 1]));
                                txtCB.Append("\t");
                            }
                            if (!Convert.IsDBNull(rowV1[i - 1])) txtCB.Append((string)(rowV1[i - 1]));
                            txtCB.Append("\n");
                        }
                        Clipboard.SetDataObject(txtCB.ToString(), true);
                        break;
                    }
            }
        }
    }
}
